﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SuiYuan.UI.Controls.Notify
{
    /// <summary>
    /// LayUIToast.xaml 的交互逻辑
    /// </summary>
    public partial class LayUIToast : Window
    {
        public int Timeout { get; set; }
        public string Text { get; set; }

        public int TotalWidth
        {
            get
            {
                return Text.Length * 14 + 30;
            }
        }

        public LayUIToast()
        {
            InitializeComponent();
            this.Loaded += LayUIToast_Loaded;
        }

        private void LayUIToast_Loaded(object sender, RoutedEventArgs e)
        {
            main.Width = TotalWidth;
            //一段时间后自动关闭
            Task.Factory.StartNew(delegate
            {
                Thread.Sleep(Timeout);
                //Invoke到主进程中去执行
                this.Dispatcher.Invoke(delegate
                {
                    this.Close();
                });
            });
        }

        public static void Show(string text, ToastLocation toastLocation, int timeout = 1000)
        {
            LayUIToast toast = new LayUIToast();
            toast.Timeout = timeout;
            toast.Text = text;
            toast.ToastText.Text = text;
            double width = SystemParameters.WorkArea.Width;
            double height = SystemParameters.WorkArea.Height;
            double toastHeight = 28;
            toast.WindowStartupLocation = WindowStartupLocation.Manual;
            if (toastLocation == ToastLocation.Center)
            {
                toast.main.Left = (width - toast.TotalWidth) / 2;
                toast.main.Top = (height - toastHeight) / 2;
            }
            else if (toastLocation == ToastLocation.Top)
            {
                toast.main.Left = (width - toast.TotalWidth) / 2;
                toast.main.Top = 100;
            }
            else if (toastLocation == ToastLocation.Bottom)
            {
                toast.main.Left = (width - toast.TotalWidth) / 2;
                toast.main.Top = height - 100 - toastHeight;
            }
            else if (toastLocation == ToastLocation.Left)
            {
                toast.main.Left = 100;
                toast.main.Top = (height - toastHeight) / 2;
            }
            else if (toastLocation == ToastLocation.Right)
            {
                toast.main.Left = width - toast.TotalWidth - 100;
                toast.main.Top = (height - toastHeight) / 2;
            }
            toast.Show();
        }
    }

    public enum ToastLocation
    {
        Top,
        Left,
        Right,
        Bottom,
        Center
    }
}
